package com.bowen.security.rsa.factory;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.bowen.security.rsa.model.MobilePlantformAuth;
import com.bowen.security.rsa.model.MysqlRsaDataSourceProperties;
import com.bowen.util.DataSourceInterface;
import com.bowen.util.impl.DataSourceImpl;

/**
 * 获取秘钥对信息
 * 
 * @author Bowen
 * @since 2021-12-06 18:15:02
 */
public class RasFactoryExcute {

  public static final String sql = "SELECT * FROM ydkyimport.mobile_plantform_auth";

  private static DataSourceInterface dao = getDao();

  private boolean mysql = false;

  /**
   * 实例
   */
  private static volatile RasFactoryExcute INSTANCE;

  /**
   * 公钥库
   */
  private Map<String, MobilePlantformAuth> authContext;

  /**
   * 私钥串
   */
  private String privateKey;

  public static DataSourceInterface getDao() {
    if (dao == null) {
      synchronized (RasFactoryExcute.class) {
        if (dao == null) {
          dao = new DataSourceImpl();
        }
      }
    }
    return dao;
  }

  /**
   * 初始化。 ip作为key，bean作为value
   * 
   * @param url             连接url
   * @param username        账号
   * @param password        密码
   * @param driverClassName 驱动
   * @return RasFactoryExcute.class
   * @throws Exception Exception
   */
  public static RasFactoryExcute init(String url, String username, String password, String driverClassName)
      throws Exception {
    List<MobilePlantformAuth> mobilePlantformAuths = getRsaData(url, username, password, driverClassName);

    if (INSTANCE == null) {
      // 保证单例
      synchronized (RasFactoryExcute.class) {
        if (INSTANCE == null) {
          INSTANCE = new RasFactoryExcute();
          // AUTH 参数初始化
          INSTANCE.setAuthContext(mobilePlantformAuths.stream()
              .collect(Collectors.toMap(MobilePlantformAuth::getSystem_code, ydkyPaymentSite -> ydkyPaymentSite)));
          INSTANCE.setPrivateKey(mobilePlantformAuths.get(0).getPlantform_private_key());
        }
      }
    }
    return INSTANCE;
  }

  /**
   * 获取私钥
   * 
   * @return 私钥
   */
  public static String getPrivatek() {
    return RasFactoryExcute.INSTANCE.getPrivateKey();
  }

  /**
   * 获取公钥
   * 
   * @param appid 系统id
   * @return 公钥
   */
  public static String getPublick(String appid) {
    return RasFactoryExcute.INSTANCE.getAuthContext().get(appid).getPublic_key();
  }

  /**
   * 获取 权限信息集
   * 
   * @param appid 系统id
   * @return 权限信息集 MobilePlantformAuth
   */
  public static MobilePlantformAuth getAuth(String appid) {
    return RasFactoryExcute.INSTANCE.getAuthContext().get(appid);
  }

  public static RasFactoryExcute getINSTANCE() {
    return INSTANCE;
  }

  public static void setINSTANCE(RasFactoryExcute iNSTANCE) {
    INSTANCE = iNSTANCE;
  }

  public Map<String, MobilePlantformAuth> getAuthContext() {
    return authContext;
  }

  public void setAuthContext(Map<String, MobilePlantformAuth> authContext) {
    this.authContext = authContext;
  }

  public String getPrivateKey() {
    return privateKey;
  }

  public void setPrivateKey(String privateKey) {
    this.privateKey = privateKey;
  }

  /**
   * 数据库查询
   * 
   * @param url             连接
   * @param username        账号
   * @param passwrd         密码
   * @param driverClassName 驱动
   * @return List<MobilePlantformAuth>
   * @throws Exception
   */
  private static List<MobilePlantformAuth> getRsaData(String url, String username, String passwrd,
      String driverClassName) throws Exception {
    return dao.query(new MysqlRsaDataSourceProperties(url, username, passwrd, driverClassName),
        MobilePlantformAuth.class, sql);
  }

  public boolean isMysql() {
    return mysql;
  }

  public void setMysql(boolean mysql) {
    this.mysql = mysql;
  }

}
